package com.arpost.pages;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnShowListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.ColorDrawable;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.hardware.Camera.Size;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.qualcomm.vuforia.samples.VideoPlayback.R;

public class TakePhoto extends Activity implements Callback {

	private final static String TAG = "TakePhoto";

	private final static int POS_ADJUST = 0;

	public static Bitmap BadIdeaBitmapForTransmission;

	SurfaceView surfaceView;
	SurfaceHolder surfaceHolder;

	Camera mCamera;
	AutoFocusCallback cb;

	RelativeLayout rabbitView, turtleView;
	ImageView rabbitImage, turtleImage;
	ImageView rabbitTrophy, turtleTrophy;
	OnTouchListener touchListener;
	OnDragListener dragListener;

	Button goBackButton, aboutInfoButton;
	ImageView takePictureButton, switchCameraButton;

	// for camera settings, no use now :(
	List<Size> mSupportedPictureSize;
	List<Size> mSupportedPreviewSize;

	int surfaceViewWidth, surfaceViewHeight;
	int pictureWidth, pictureHeight;
	int previewWidth, previewHeight;
	int mRotateDegree = 90;

	int mCameraSide = Camera.CameraInfo.CAMERA_FACING_FRONT;

	// PNG sequence
	int mRabbitAnimIndex, mTurtleAnimIndex;
	// AnimationController mRabbitAnimation, mTurtleAnimation;
	static int[] mRabbitSrc = { R.drawable.rabbit_01_00000, R.drawable.rabbit_02_00000, R.drawable.rabbit_03_00000, R.drawable.rabbit_04_00000 };
	static int[] mTurtleSrc = { R.drawable.turtle_01_00000, R.drawable.turtle_02_00000, R.drawable.turtle_03_00000, R.drawable.turtle_04_00000 };

	// Share util
	Dialog mShareDialog;
	ShareUtils mShareUtils;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.take_photo);

		this.setSurfaceViewWidthHeight();

		// init camera
		// this.setCamera();
		// mCamera.startPreview();
		mCameraSide = Camera.CameraInfo.CAMERA_FACING_BACK;

		// init share utils
		this.setupShareStuff();

		// set drag event
		touchListener = new DragTouchListener();
		rabbitView = (RelativeLayout) findViewById(R.id.rabbit_view);
		turtleView = (RelativeLayout) findViewById(R.id.turtle_view);
		rabbitImage = (ImageView) findViewById(R.id.rabbit_image);
		// WebView rabbitImage = (WebView) findViewById(R.id.rabbit_image);
		// rabbitImage.loadUrl("file:///assets/gif/rabbit_1.html");

		turtleImage = (ImageView) findViewById(R.id.turtle_image);
		rabbitView.setOnTouchListener(touchListener);
		turtleView.setOnTouchListener(touchListener);

		// surface view for camera
		surfaceView = (SurfaceView) findViewById(R.id.take_photo_camera_view);
		surfaceHolder = surfaceView.getHolder();
		surfaceHolder.addCallback(this);
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

		// init buttons
		goBackButton = (Button) findViewById(R.id.button_go_back);
		takePictureButton = (ImageView) findViewById(R.id.button_take_picture);
		switchCameraButton = (ImageView) findViewById(R.id.take_photo_button_switch);
		aboutInfoButton = (Button) findViewById(R.id.button_about_info);
		rabbitTrophy = (ImageView) findViewById(R.id.rabbit_award);
		turtleTrophy = (ImageView) findViewById(R.id.turtle_award);
		this.setButtonClickListener();

		// activate png sequence
		mRabbitAnimIndex = mTurtleAnimIndex = 0;
		setCharacterImage();
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		try {
			this.setCamera();
			mCamera.setPreviewDisplay(surfaceHolder);
			mCamera.startPreview();
			// mCamera.autoFocus(autoFocusCallback());
		} catch (IOException e) {
			// left blank for now
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		Log.d("TakePhoto", width + " : " + height);
		// start preview with new settings
		try {
			mCamera.setPreviewDisplay(surfaceHolder);
			Camera.Parameters params = mCamera.getParameters();
			// params.setPictureSize(width, height);
			mCamera.setParameters(params);
			mCamera.startPreview();
			// mCamera.autoFocus(autoFocusCallback());
		} catch (Exception e) {
			// intentionally left blank for a test
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		if (mCamera != null) {
			mCamera.stopPreview();
			mCamera.release();
			mCamera = null;
		}
	}

	ImageView cancel_button, wx_share_button, wb_share_button;

	private void setupShareStuff() {
		mShareUtils = new ShareUtils(this);
		mShareDialog = new Dialog(this);
		mShareDialog.setContentView(R.layout.share_dialog);
		mShareDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

		cancel_button = (ImageView) mShareDialog.findViewById(R.id.button_share_dialog_cancel);
		wx_share_button = (ImageView) mShareDialog.findViewById(R.id.button_share_dialog_wx);
		wb_share_button = (ImageView) mShareDialog.findViewById(R.id.button_share_dialog_wb);

		cancel_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mShareDialog.dismiss();
			}
		});

		wx_share_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				final File image_file = ShareUtils.getBitmap("share_dialog.png");
				mShareUtils.shareToWXTimeLine("test wx", image_file);
			}
		});

		wb_share_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				final File image_file = ShareUtils.getBitmap("share_dialog.png");
				mShareUtils.shareToWB("test wb", image_file);
			}
		});

		mShareDialog.setOnShowListener(new OnShowListener() {
			@Override
			public void onShow(DialogInterface arg0) {
				RelativeLayout v = (RelativeLayout) mShareDialog.findViewById(R.id.share_dialog_parent);
				/*
				 * cancel_button.post(new Runnable() {
				 * 
				 * @Override public void run() { v.removeView(cancel_button); }
				 * });
				 */
				v.removeView(cancel_button);
				Bitmap bitmap = Bitmap.createBitmap(900, 430, Bitmap.Config.RGB_565);
				v.draw(new Canvas(bitmap));
				ShareUtils.saveBitmap(bitmap, "share_dialog.png");
				v.addView(cancel_button);
			}
		});

	}

	private void setButtonClickListener() {
		goBackButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String mClassToLaunchPackage = "com.qualcomm.vuforia.samples.VideoPlayback";
				String mClassToLaunch = "com.qualcomm.vuforia.samples.VideoPlayback.app.VideoPlayback.VideoPlayback";

				Intent i = new Intent();
				i.setClassName(mClassToLaunchPackage, mClassToLaunch);
				startActivity(i);
				System.exit(0);
			}
		});

		takePictureButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// mPNGHandler.removeCallbacks(mPNGRunnable);
				// mRabbitPNG = mTurtlePNG = null;
				mCamera.autoFocus(autoFocusCallback());
			}
		});

		switchCameraButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				switchCameraSide();
			}
		});

		aboutInfoButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(TakePhoto.this, AboutInfo.class);
				startActivity(intent);
			}
		});

		rabbitTrophy.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				RequestTask r = new RequestTask();
				r.execute("0");
			}
		});

		turtleTrophy.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				RequestTask r = new RequestTask();
				r.execute("1");
			}
		});
	}

	// id
	// - 0 for rabbit
	// - 1 for turtle
	private int mVoteId = -1;

	private void setVoted(int id) {

		mVoteId = id;

		if (mVoteId == 0) {
			turtleImage.setVisibility(View.INVISIBLE);
		} else {
			rabbitImage.setVisibility(View.INVISIBLE);
		}
		rabbitTrophy.setVisibility(View.INVISIBLE);
		turtleTrophy.setVisibility(View.INVISIBLE);
	}

	private void setupPNGSequence() {
		mRabbitAnimIndex = mTurtleAnimIndex = 0;

		/*
		 * if (mRabbitAnimation == null) { mRabbitAnimation = new
		 * AnimationController(rabbitImage, getApplicationContext()); } if
		 * (mTurtleAnimation == null) { mTurtleAnimation = new
		 * AnimationController(turtleImage, getApplicationContext()); }
		 * 
		 * mRabbitAnimation.setPNGSequence(AnimationController.getRabbitRes(
		 * mRabbitAnimIndex));
		 * mTurtleAnimation.setPNGSequence(AnimationController
		 * .getTurtleRes(mTurtleAnimIndex));
		 * 
		 * mRabbitAnimation.start(); mTurtleAnimation.start();
		 * 
		 * /* mRabbitTotal = mTurtleTotal = 4; mRabbitCount = mTurtleCount = 0;
		 * 
		 * setCharacterImage(rabbitImage, mRabbitPNGIds, mRabbitCount);
		 * setCharacterImage(turtleImage, mTurtlePNGIds, mTurtleCount);
		 */
	}

	private void setCharacterImage() {
		rabbitImage.setImageResource(mRabbitSrc[mRabbitAnimIndex]);
		turtleImage.setImageResource(mTurtleSrc[mTurtleAnimIndex]);
	}

	private void setCharacterImage(ImageView v, int[] png, int index) {
		v.setBackgroundResource(R.drawable.rabbit_01_00000);
		/*
		 * AnimationDrawable anim = (AnimationDrawable) v.getBackground(); if
		 * (anim != null && anim.isRunning()) anim.stop();
		 * v.setBackgroundResource(png[index]);
		 * v.setScaleType(ScaleType.CENTER_INSIDE); anim = (AnimationDrawable)
		 * v.getBackground(); anim.start();
		 */
	}

	private void switchCameraSide() {
		try {
			if (mCamera != null) {
				mCamera.stopPreview();
				mCamera.release();
				mCamera = null;
				if (mCameraSide == Camera.CameraInfo.CAMERA_FACING_BACK) {
					mCameraSide = Camera.CameraInfo.CAMERA_FACING_FRONT;
				} else {
					mCameraSide = Camera.CameraInfo.CAMERA_FACING_BACK;
				}

				setCamera();
				mCamera.setPreviewDisplay(surfaceHolder);
				mCamera.startPreview();
			}
		} catch (Exception e) {
			// if the switch fail
		}
	}

	private void setCamera() {
		if (mCamera == null)
			mCamera = Camera.open(mCameraSide);

		/*
		 * android.hardware.Camera.CameraInfo info = new
		 * android.hardware.Camera.CameraInfo(); int rotation =
		 * getWindowManager().getDefaultDisplay().getRotation(); int degrees =
		 * 0; switch (rotation) { case Surface.ROTATION_0: degrees = 0; break;
		 * case Surface.ROTATION_90: degrees = 90; break; case
		 * Surface.ROTATION_180: degrees = 180; break; case
		 * Surface.ROTATION_270: degrees = 270; break; }
		 * 
		 * int result; if (mCameraSide == Camera.CameraInfo.CAMERA_FACING_FRONT)
		 * { result = (info.orientation + degrees) % 360; result = (360 -
		 * result) % 360; // compensate the mirror } else { // back-facing
		 * result = (info.orientation - degrees + 360) % 360; }
		 * 
		 * mRotateDegree = result; mCamera.setDisplayOrientation(result);
		 */

		if (mCameraSide == Camera.CameraInfo.CAMERA_FACING_BACK) {
			mCamera.setDisplayOrientation(mRotateDegree);
		} else {
			mCamera.setDisplayOrientation(mRotateDegree);
		}

		try {
			Camera.Parameters parameters = mCamera.getParameters();
			parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);

			mSupportedPictureSize = parameters.getSupportedPictureSizes();
			int len = mSupportedPictureSize.size() / 4;
			pictureWidth = mSupportedPictureSize.get(len).width;
			pictureHeight = mSupportedPictureSize.get(len).height;
			Log.i(TAG, "supported picture size min: " + len + ":" + pictureWidth + ":" + pictureHeight);
			parameters.setPictureSize(pictureWidth, pictureHeight);
			if (mCameraSide == Camera.CameraInfo.CAMERA_FACING_FRONT) {
				parameters.setRotation(270);
			}
			mCamera.setParameters(parameters);
		} catch (Exception e) {
			e.printStackTrace();
			// prevent setParamter fail exception
		}

		/*
		 * mSupportedPreviewSize = parameters.getSupportedPreviewSizes();
		 * previewWidth = mSupportedPreviewSize.get(0).width; previewHeight =
		 * mSupportedPreviewSize.get(0).height;
		 * parameters.setPreviewSize(previewWidth, previewHeight);
		 * 
		 * mCamera.setParameters(parameters);
		 */
	}

	private Bitmap scaleBitmap(Bitmap bmp, int sw, int sh, int lw, int lh) {
		int origin_width = rabbitView.getWidth();
		int origin_height = rabbitView.getHeight();
		int render_width = origin_width * lw / sw;
		int render_height = origin_height * lh / sh;

		int real_width = bmp.getWidth();
		int real_height = bmp.getHeight();

		Matrix m = new Matrix();
		float scaleWidth = ((float) render_width) / ((float) real_width);
		float scaleHeight = scaleWidth;
		m = new Matrix();
		m.postScale(scaleWidth, scaleHeight);

		Bitmap new_bitmap = Bitmap.createBitmap(bmp, 0, 0, real_width, real_height, m, true);

		return new_bitmap;
	}

	private void setSurfaceViewWidthHeight() {
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		int height = displaymetrics.heightPixels;
		int width = displaymetrics.widthPixels;
		surfaceViewWidth = width;
		surfaceViewHeight = height;
	}

	private AutoFocusCallback autoFocusCallback() {
		AutoFocusCallback cb = new AutoFocusCallback() {
			@Override
			public void onAutoFocus(boolean arg0, Camera arg1) {
				mCamera.takePicture(new ShutterCallback() {
					@Override
					public void onShutter() {
					}
				}, null, new PictureCallback() {
					@Override
					public void onPictureTaken(byte[] data, Camera cam) {
						Bitmap camera_bmp;
						BitmapFactory.Options options = new BitmapFactory.Options();
						options.inMutable = true;
						Log.i(TAG, "picture taken data length " + data.length);
						camera_bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options);

						// rotate degrees
						Matrix m = new Matrix();
						if (mCameraSide == Camera.CameraInfo.CAMERA_FACING_BACK) {
							m.setRotate(mRotateDegree, (float) camera_bmp.getWidth() / 2, (float) camera_bmp.getHeight() / 2);
						} else {
							m.setRotate(0, (float) camera_bmp.getWidth() / 2, (float) camera_bmp.getHeight() / 2);
						}
						Bitmap bmp = Bitmap.createBitmap(camera_bmp, 0, 0, camera_bmp.getWidth(), camera_bmp.getHeight(), m, true);
						camera_bmp.recycle();
						Canvas canvas = new Canvas(bmp);

						// get bitmap
						int bmp_width = bmp.getWidth();
						int bmp_height = bmp.getHeight();
						Bitmap rabbit_bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(), mRabbitSrc[mRabbitAnimIndex]);
						Bitmap new_rabbit_bitmap = scaleBitmap(rabbit_bitmap, surfaceViewWidth, surfaceViewHeight, bmp_width, bmp_height);
						Bitmap turtle_bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(), mTurtleSrc[mTurtleAnimIndex]);
						Bitmap new_turtle_bitmap = scaleBitmap(turtle_bitmap, surfaceViewWidth, surfaceViewHeight, bmp_width, bmp_height);

						// draw rabbit and turtle relative to center
						int surface_center_width = surfaceViewWidth / 2;
						int surface_center_height = surfaceViewHeight / 2;
						int bitmap_center_width = bmp_width / 2;
						int bitmap_center_height = bmp_height / 2;
						if (mVoteId == 0 || mVoteId == -1) {
							// draw rabbit
							int left = (rabbitView.getLeft());
							int top = (rabbitView.getTop());
							int offset_x = left - surface_center_width;
							int offset_y = top - surface_center_height;
							float new_x = ((float) (offset_x * bmp_width)) / (float) surfaceViewWidth - POS_ADJUST;
							float new_y = ((float) (offset_y * bmp_height)) / (float) surfaceViewHeight - POS_ADJUST;
							left = (int) new_x + bitmap_center_width;
							top = (int) new_y + bitmap_center_height;
							canvas.drawBitmap(new_rabbit_bitmap, left, top, null);
						}

						if (mVoteId == 1 || mVoteId == -1) {
							// draw turtle
							int left = (turtleView.getLeft());
							int top = (turtleView.getTop());
							int offset_x = left - surface_center_width;
							int offset_y = top - surface_center_height;
							float new_x = ((float) (offset_x * bmp_width)) / (float) surfaceViewWidth;
							float new_y = ((float) (offset_y * bmp_height)) / (float) surfaceViewHeight;
							left = (int) new_x + bitmap_center_width;
							top = (int) new_y + bitmap_center_height;
							canvas.drawBitmap(new_turtle_bitmap, left, top, null);
						}

						try {
							BadIdeaBitmapForTransmission.recycle();
						} catch (Exception e) {
							// recycle failed
						}
						BadIdeaBitmapForTransmission = bmp;

						Intent intent = new Intent(TakePhoto.this, ShowPhoto.class);
						startActivity(intent);
					}
				});

				Log.i("TakePhoto", "focus a photo");
			}
		};
		return cb;
	}

	private class DragTouchListener implements OnTouchListener {

		int startX, startY;
		int lastX, lastY;

		public boolean onTouch(View v, MotionEvent event) {

			int ea = event.getAction();

			switch (ea) {
			case MotionEvent.ACTION_DOWN:
				startX = lastX = (int) event.getRawX();
				startY = lastY = (int) event.getRawY();
				return true;

			case MotionEvent.ACTION_MOVE:
				int dx = (int) event.getRawX() - lastX;
				int dy = (int) event.getRawY() - lastY;
				int left = v.getLeft() + dx;
				int top = v.getTop() + dy;
				int right = v.getRight() + dx;
				int bottom = v.getBottom() + dy;
				if (left < 0) {
					left = 0;
					right = left + v.getWidth();
				}
				if (right > surfaceViewWidth) {
					right = surfaceViewWidth;
					left = right - v.getWidth();
				}
				if (top < 0) {
					top = 0;
					bottom = top + v.getHeight();
				}
				if (bottom > surfaceViewHeight) {
					bottom = surfaceViewHeight;
					top = bottom - v.getHeight();
				}
				LayoutParams params = (LayoutParams) v.getLayoutParams();
				params.setMargins(left, top, 0, 0);
				v.setLayoutParams(params);
				// v.layout(left, top, right, bottom);

				lastX = (int) event.getRawX();
				lastY = (int) event.getRawY();
				return true;

			case MotionEvent.ACTION_UP:
				int ddx = (int) event.getRawX() - startX;
				int ddy = (int) event.getRawY() - startY;
				int dist = ddx * ddx + ddy * ddy;
				if (dist < 400) {
					if (v.getId() == R.id.rabbit_view) {
						mRabbitAnimIndex += 1;
						if (mRabbitAnimIndex == 4)
							mRabbitAnimIndex = 0;
						setCharacterImage();
						/*
						 * mRabbitAnimation.stop();
						 * mRabbitAnimation.setPNGSequence
						 * (AnimationController.getRabbitRes(mRabbitAnimIndex));
						 * mRabbitAnimation.start();
						 */

					} else {
						mTurtleAnimIndex += 1;
						if (mTurtleAnimIndex == 4)
							mTurtleAnimIndex = 0;
						setCharacterImage();
						/*
						 * mTurtleAnimation.stop();
						 * mTurtleAnimation.setPNGSequence
						 * (AnimationController.getTurtleRes(mTurtleAnimIndex));
						 * mTurtleAnimation.start();
						 */
						/*
						 * mTurtleCount += 1; if (mTurtleCount >= mTurtleTotal)
						 * mTurtleCount = 0; setCharacterImage(turtleImage,
						 * mTurtlePNGIds, mTurtleCount);
						 */
					}
				}
				return false;
			}
			return false;
		}
	}

	private String mShareString = "";
	private String mBaseShareString = "#������������#\n������������������������������%d%%������������������������������%s������������������������������\n������������������������������������������������������������������������������������������������������������������������������";

	class RequestTask extends AsyncTask<String, String, Integer> {

		private String mCharacter = "";
		private String mPercent = "";
		private int mVoteId = -1;

		@Override
		protected Integer doInBackground(String... uri) {
			HttpClient httpclient = new DefaultHttpClient();
			HttpResponse response;
			String responseString = null;
			try {
				if (uri[0] == "0") {
					mCharacter = "������";
					mVoteId = 0;
				} else {
					mCharacter = "������";
					mVoteId = 1;
				}
				String url = "http://114.215.201.178:8000/ludic_science/numcount?name=";
				response = httpclient.execute(new HttpGet(url + uri[0]));
				StatusLine statusLine = response.getStatusLine();
				if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
					ByteArrayOutputStream out = new ByteArrayOutputStream();
					response.getEntity().writeTo(out);
					out.close();
					responseString = out.toString();
				} else {
					// Closes the connection.
					response.getEntity().getContent().close();
					throw new IOException(statusLine.getReasonPhrase());
				}

				String[] result = responseString.split(":");
				if (result.length >= 2 && result[0].equals("success")) {
					return Integer.parseInt(result[1]);
				} else {
					return -1;
				}
			} catch (Exception e) {
				return -1;
			}
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);

			if (result != -1) {
				mShareString = String.format(mBaseShareString, result, mCharacter);
				TextView percent = (TextView) mShareDialog.findViewById(R.id.text_share_dialog_percent);
				TextView character = (TextView) mShareDialog.findViewById(R.id.text_share_dialog_character);
				percent.setText(result + "%");
				character.setText(mCharacter);

				setVoted(mVoteId);
				mShareDialog.show();
			} else {
				new AlertDialog.Builder(TakePhoto.this).setTitle("������������").setMessage("���������������������������������������������")
						.setNeutralButton("������", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dlg, int sumthin) {
								try {
									dlg.dismiss();
								} catch (Exception e) {
								}
							}
						}).show();
			}

		}
	}

	private void showAll() {
		rabbitImage.setVisibility(View.VISIBLE);
		turtleImage.setVisibility(View.VISIBLE);
		rabbitTrophy.setVisibility(View.VISIBLE);
		turtleTrophy.setVisibility(View.VISIBLE);
	}

	protected void onPause() {
		super.onPause();
		mCamera.release();
		mCamera = null;
	}

	protected void onResume() {
		super.onResume();
		this.setCamera();
		mCamera.startPreview();

		showAll();
		setCharacterImage();
		mVoteId = -1;
	}
}
